package com.education.ai.controller;

import com.education.ai.entity.KnowledgePoint;
import com.education.ai.service.KnowledgePointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 知识点控制器
 */
@RestController
@RequestMapping("/api/knowledge")
@CrossOrigin(origins = {"http://localhost:3000", "http://localhost:8080", "http://localhost:8081"}, allowCredentials = "true")
public class KnowledgeController {
    
    @Autowired
    private KnowledgePointService knowledgePointService;
    
    /**
     * 获取所有知识点
     */
    @GetMapping
    public ResponseEntity<List<KnowledgePoint>> getAllKnowledgePoints() {
        return ResponseEntity.ok(knowledgePointService.getAllKnowledgePoints());
    }
    
    /**
     * 根据ID获取知识点
     */
    @GetMapping("/{id}")
    public ResponseEntity<?> getKnowledgePointById(@PathVariable Long id) {
        return knowledgePointService.getKnowledgePointById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }
    
    /**
     * 根据学科获取知识点
     */
    @GetMapping("/subject/{subject}")
    public ResponseEntity<List<KnowledgePoint>> getKnowledgePointsBySubject(@PathVariable String subject) {
        return ResponseEntity.ok(knowledgePointService.getKnowledgePointsBySubject(subject));
    }
    
    /**
     * 根据学科和年级获取知识点
     */
    @GetMapping("/subject/{subject}/grade/{grade}")
    public ResponseEntity<List<KnowledgePoint>> getKnowledgePointsBySubjectAndGrade(
            @PathVariable String subject, @PathVariable String grade) {
        return ResponseEntity.ok(knowledgePointService.getKnowledgePointsBySubjectAndGrade(subject, grade));
    }
    
    /**
     * 获取知识点层次结构
     */
    @GetMapping("/hierarchy/{subject}")
    public ResponseEntity<Map<Long, List<KnowledgePoint>>> getKnowledgeHierarchy(@PathVariable String subject) {
        return ResponseEntity.ok(knowledgePointService.getKnowledgeHierarchy(subject));
    }
    
    /**
     * 获取子知识点
     */
    @GetMapping("/children/{parentId}")
    public ResponseEntity<List<KnowledgePoint>> getChildKnowledgePoints(@PathVariable Long parentId) {
        return ResponseEntity.ok(knowledgePointService.getChildKnowledgePoints(parentId));
    }
    
    /**
     * 搜索知识点
     */
    @GetMapping("/search")
    public ResponseEntity<List<KnowledgePoint>> searchKnowledgePoints(
            @RequestParam String name, @RequestParam String subject) {
        return ResponseEntity.ok(knowledgePointService.searchKnowledgePoints(name, subject));
    }
    
    /**
     * 创建知识点
     */
    @PostMapping
    public ResponseEntity<KnowledgePoint> createKnowledgePoint(@RequestBody KnowledgePoint knowledgePoint) {
        KnowledgePoint createdPoint = knowledgePointService.saveKnowledgePoint(knowledgePoint);
        return ResponseEntity.status(HttpStatus.CREATED).body(createdPoint);
    }
    
    /**
     * 更新知识点
     */
    @PutMapping("/{id}")
    public ResponseEntity<?> updateKnowledgePoint(
            @PathVariable Long id, @RequestBody KnowledgePoint knowledgePoint) {
        return knowledgePointService.getKnowledgePointById(id)
                .map(existingPoint -> {
                    knowledgePoint.setId(id);
                    return ResponseEntity.ok(knowledgePointService.saveKnowledgePoint(knowledgePoint));
                })
                .orElse(ResponseEntity.notFound().build());
    }
    
    /**
     * 删除知识点
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteKnowledgePoint(@PathVariable Long id) {
        return knowledgePointService.getKnowledgePointById(id)
                .map(point -> {
                    knowledgePointService.deleteKnowledgePoint(id);
                    return ResponseEntity.ok().build();
                })
                .orElse(ResponseEntity.notFound().build());
    }
} 